/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/igfx/FileGfxInterface.h>
#include <pgl/zlib/gzip.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace gfxinterface
{

CFileGfxInterface::CFileGfxInterface()
: CGfxInterface()
{
}

CFileGfxInterface::~CFileGfxInterface()
{
}

void CFileGfxInterface::BeginPaint(HDC hDC )
{
	CGfxInterface::BeginPaint(hDC);

	m_stream.clear();
}

void CFileGfxInterface::EndPaint()
{
	m_stream.flush();

	CGfxInterface::EndPaint();
}

void CFileGfxInterface::SetCreator(LPCTSTR str)
{
	m_sCreator=str;
}

void CFileGfxInterface::SetTitle(LPCTSTR str)
{
	m_sTitle=str;
}

LPCTSTR CFileGfxInterface::GetCreator() const
{
	return m_sCreator.c_str();
}
LPCTSTR CFileGfxInterface::GetTitle() const
{
	return m_sTitle.c_str();
}

bool CFileGfxInterface::Save(LPCTSTR szFileName, bool bZipped)
{
	using namespace std;
	using namespace zlib;
	USES_CONVERSION;

	if (!szFileName)
		return false;

	_tstring ext(_T("."));
	ext+=GetFileExt();
	std::_tstring sFileName(szFileName);

	// testing if right extension..
	if (sFileName.rfind(ext) != sFileName.size() - ext.size())
	{
		sFileName+=ext;
	}

	if (bZipped)
	{
		CGZip zipper;

		if( ! zipper.Open( sFileName.c_str(), CGZip::ArchiveModeWrite))
			return false;
		if ( ! zipper.WriteString( m_stream.str().c_str() ) )
			return false;
		return zipper.Close();

/*
			time_t t;
			std::_tstring str=(_T("Title: ")+m_sTitle)+_T(", Creator: ")+m_sCreator;

			// preparing header
			CZipFileHeader zipHeader;
			zipHeader.SetFileName(szFileName);
			time(&t);
			zipHeader.SetTime(t);
			zipHeader.SetComment(str.c_str()); 
		
			// creating archive
			CZipArchive zipAr;
			zipAr.Open(szFileName, CZipArchive::zipCreate);
			if (!zipAr.OpenNewFile( zipHeader, Z_BEST_COMPRESSION))
				return false;
	
			zipAr.WriteNewFile( m_stream.str().c_str() , m_stream.str().size()*sizeof(TCHAR) );
	
			zipAr.Close();
*/
	}
	else
	{
		_tofstream outFile;
		outFile.open( T2CA(sFileName.c_str()) );
		if (!outFile.is_open())
			return false;
		outFile<<m_stream.str().c_str();
		outFile.close();

	};

	return true;
}

void CFileGfxInterface::GetString(std::_tstring& str) const
{
	str=m_stream.str();
}

void CFileGfxInterface::GetString(CString& str) const
{
	str=m_stream.str().c_str();
}

}
